//
//  FBtMessageModel.m
//  JwPack
//
//  Created by 陈警卫 on 2025/7/28.
//

#import "FBtMessageModel.h"
#import "JwPack-Swift.h"

@interface FBtMessageModel ()

/// 超时回调(内部用)
@property (nonatomic, copy) void(^didTimeoutAction)(FBtMessageModel *messageModel);

@end


@implementation FBtMessageModel

- (void)dealloc {
    kFBtLog(@"dealloc--%@", self);
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.state = FBtMessageStatePlain;
        self.level = FBtMessageLevelPlain;
        self.writeType = FBtMessageWriteTypeWithoutResponse;
        self.timeout = 1.0;
        self.signal = FBtSig_plain;
    }
    return self;
}

/// 配置完成数据
- (void)setupCompleData {
    
    if (self.signal == FBtSig_plain) {
        return;
    }
    UInt16 prefix = 0x7F7F;
    UInt16 suffix = 0xF7F7;
    
    if (self.signal == FBtSig_handshakeAA ||
        self.signal == FBtSig_handshakeAC ||
        self.signal == FBtSig_handshakeAB) {
        prefix = 0x7E7E;
        suffix = 0xE7E7;
    }
    
    NSData *prefixData = [NSData dataWithBytes:&prefix length:sizeof(prefix)];
    NSData *suffixData = [NSData dataWithBytes:&suffix length:sizeof(suffix)];
    
    UInt8 func = (self.signal >> 8) & 0xFF;
    UInt8 type = self.signal & 0xFF;
    self.func = func;
    self.type = type;
    
    if (self.valueData) {
        self.length = self.valueData.length;
    }
    if (type != 0x00) {
        self.length = self.length + 1;
    }
    UInt16 length = CFSwapInt16HostToBig(self.length);

    NSData *funcData = [NSData dataWithBytes:&func length:sizeof(func)];
    NSData *typeData = [NSData dataWithBytes:&type length:sizeof(type)];
    self.timeData = [NSDate f_packetIDData];
    NSData *lengthData = [NSData dataWithBytes:&length length:sizeof(length)];
    
    NSMutableData *messageData = [NSMutableData data];
    [messageData appendData:prefixData];
    [messageData appendData:funcData];
    [messageData appendData:self.timeData];
    [messageData appendData:lengthData];
    if (type != 0x00) {
        [messageData appendData:typeData];
    }
    [messageData appendData:self.valueData];
    
    NSData *crcRawData = [messageData subdataWithRange:(NSMakeRange(2, messageData.length - 2))];
    UInt16 crc = [CJCRCHub computeCRC16WithData:crcRawData type:(CJCRC16TypeModbus)];
    NSData *crcData = [NSData dataWithBytes:&crc length:sizeof(crc)];
    [messageData appendData:crcData];
    [messageData appendData:suffixData];
    
    self.data = messageData;
    self.key = [self.timeData f_hexString];
}


- (void)startTimer:(void(^)(FBtMessageModel *messageModel))didTimeoutAction {
    self.didTimeoutAction = didTimeoutAction;
    if (self.timeout > 0) {
        __weak typeof(self) wself = self;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:self.timeout repeats:NO block:^(NSTimer * _Nonnull timer) {
            [wself timerAction];
        }];
    }
}

- (void)timerAction {
    if (self.r_data == nil) {
        self.state = FBtMessageStateTimeout;
        if (self.didTimeoutAction) {
            self.didTimeoutAction(self);
        }
    }
}


@end
